<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2021-01-03
  Time: 21:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>采购单管理</title>
    <base href="<%=request.getContextPath()%>">
    <link rel="stylesheet" href="/webjars/layui/2.5.6/css/layui.css"/>
    <style type="text/css">
        .layui-table-page {
            position:fixed;
        }
    </style>
</head>
<body>
<form class="layui-form" lay-filter="search">
    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label" style="width: 100px">采购单号</label>
            <div class="layui-input-inline" style="width: 120px;">
                <input  type="text" id="purchaseNo" name="purchaseNo"  class="layui-input" lay-filter="caigouId">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label" style="width: 100px">供应商</label>
            <div class="layui-input-inline" style="width: 120px;">
                <input  type="text" id="supplyCode" name="supplyCode"  class="layui-input" lay-filter="gongyingshang">
            </div>
        </div>
        <div class="layui-inline">
            <select name="purchaseStatus" lay-filter="caigou" lay-verify="">
                <option value="null">请选择采购状态</option>
                <option value="0">未收货</option>
                <option value="1">已收货</option>
            </select>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn layui-btn-sm" onclick="add()" >添加</button>
        </div>

    </div>
</form>
<script type="text/html" id="toolbar">


</script>
<table lay-filter="test" id="purchaseNote"></table>
<script type="text/html" id="action">

        {{#  if(d.purchaseStatus== "0"){ }}
        <a class="layui-btn layui-btn-xs" lay-event="particulars">详情</a>
        <a class="layui-btn layui-btn-xs" lay-event="update">确认收货</a>
        {{#  } }}
        {{#  if(d.purchaseStatus== "1"){ }}
        <a class="layui-btn layui-btn-xs" lay-event="particulars">详情</a>
        {{#  } }}


</script>

<script src="/webjars/layui/2.5.6/layui.js"></script>
<script>
    layui.use(["table","jquery","laypage","form"],function () {
        let table=layui.table;
        let $=layui.$;
        // let laypage = layui.laypage;
        let sea=layui.form.val("search");
        let form=layui.form;
        laypage = layui.laypage
        table.render({
            id:"purchaseNote",
            elem:"#purchaseNote",
            where:sea,
            url:"/heng/purchase/search",
            height:"400px",
            toolbar: "#toolbar",
            page:true,
            limit:5,
            limits:[5,10,15,20],
            cols:[[
                {title:"ID",field:"purchaseMasterId"},
                {title:"采购单号",field:"purchaseNo"},
                {title: "生成日期",field: "purchaseDate"},
                {title: "采购状态",field: "purchaseStatus"},
                {title: "订单状态",field: "orderStatus"},
                {title: "供应商",field: "supplyCode"},
                {title: "金额",field: "price"},
                {title: "操作",toolbar:"#action"}
            ]],
            done:function (res,curr,count) {
                //采购状态的转换
                $("[data-field='purchaseStatus']").children().each(function(){
                    if($(this).text()=='0'){
                        $(this).text("未收货")
                    }else if($(this).text()=='1'){
                        $(this).text("已收货")
                    }
                });
                //订单状态的转换
                $("[data-field='orderStatus']").children().each(function(){
                    if($(this).text()=='0'){
                        $(this).text("待确认")
                    }else if($(this).text()=='1'){
                        $(this).text("已确认")
                    } else if($(this).text()=='2'){
                        $(this).text("未通过")
                    }
                });

                // //获取表中数据
                // table.on('tool(test)',function (obj) {
                //     var data = obj.data; //获得当前行数据
                //     var id=data.purchaseMasterId;
                //     console.log(data);
                //     console.log(id)
                // })

            }

        })


        //采购订单号文本框监听事件
        $("#purchaseNo").on("input",function (e) {
            let sea=form.val("search");
            layui.table.reload('purchaseNote',{
                where: sea
            });
        });

        //供应商文本框监听事件
        $("#supplyCode").on("input",function (e) {
            let sea=form.val("search");
            layui.table.reload('purchaseNote',{
                where: sea
            });
        });

        //下拉框监听事件
        form.on('select(caigou)',function (data) {
            let sea=form.val("search");
            layui.table.reload('purchaseNote',{
                where:sea
            });
        });


        table.on("tool(test)",function ({data,event}) {

            switch (event) {
                case "update":
                    update(data);
                    break;
                case "particulars":
                    particulars(data);
                    break;
            }
        })
    })

    //出库并修改状态
    function update(data) {
        layui.use(["layer","jquery","table","form"],function () {
            let layer=layui.layer;
            let $=layui.$;
            let table=layui.table;
            let form=layui.form;

            //采购主表id
            let purchaseMasterId=data.purchaseMasterId;
            console.log("采购主表id："+purchaseMasterId)
            //根据物资编码获取物资名称
            // $.post("/heng/purchase/selectByCoCode", cgCode,function (msg) {
            //     console.log("物资："+msg.data)
            //     layui.form.val("addSTOCK",{dictionaryId:msg.cgName});
            // })
            //根据采购主表id获取采购子表数据
            $.post('/heng/purchase/selectId', {'childId':purchaseMasterId}, function(str){
                console.log(str.data)
                layui.form.val("addSTOCK",{amount:str.data.purchaseBuy,cgCode:str.data.cgCode});

            });
            //选择仓库
            form.on('select(branchCode)', function(data){
                $.post({
                    url: '/wareHouse/getTbUscWarehouseByBranch',
                    data: {"branch":data.value},
                    //dataType: 'json',
                    success: function ({data}) {
                        //使用循环遍历，给下拉列表赋值
                        $('#warehouseName option:not(:first)').remove();
                        $.each(data, function (index, value) {
                            // console.log(value.department_id);
                            //console.log(index,value);
                            $('#warehouseName').append(new Option(value.warehouseName,value.warehousId));// 下拉菜单里添加元素
                        });
                        layui.form.render("select");//重新渲染 固定写法
                    }
                })
            });
            layer.open({
                type: 1,
                title:"入库",
                content: $('#addSTOCK'), //注意，如果str是object，那么需要字符拼接。
                closeBtn:1,
                anim: 5,
                area: ['500px', '400px'],
                btn:["确认","取消"],
                yes(){

                    let stock=form.val("addSTOCK")
                    //仓库id
                    let warehousId=stock.warehousId;
                    console.log("仓库id"+warehousId)
                    //数量
                    let amount=stock.amount;
                    console.log("数量："+amount)
                    //效期
                    let xiaoqi=stock.xiaoqi;
                    console.log("效期"+xiaoqi)
                    //物资编码
                    let cgCode=stock.cgCode;
                    console.log("物资编码："+cgCode)
                    if (warehousId==null||warehousId==''||warehousId==0||xiaoqi==null||xiaoqi==''){
                        alert("不能为空，添加失败")
                        $("#addSTOCK").click();
                        return false;
                    }else {
                        $.post("/heng/purchase/addstock",{'warehousId':warehousId,'amount':amount,'xiaoqi':xiaoqi,'cgCode':cgCode})
                            .done(({msg}) => {
                                table.reload("purchaseNote");
                                layer.close(layer.index);
                                layer.msg(msg);
                            });
                        $.post("/heng/purchase/update",{'purchaseMasterId':data.purchaseMasterId})
                            .done(({msg})=>{
                                // layui.table.reload("test");
                                // layer.close(layer.index);
                                // layer.msg(msg);
                            });
                    }


                }

            })



        })
    }
    //查看详情
    function particulars(data) {
        layui.use(["layer","jquery","table"],function () {
            console.log("点击详情")
            let layer=layui.layer;
            let $=layui.$;
            let table=layui.table;

            let id=data.purchaseMasterId;
            $.post('/heng/purchase/selectId', {'childId':id}, function(str){
                console.log(str)
                layer.open({
                    type: 1,
                    title:"详细信息",
                    content: $('#particulars'), //注意，如果str是object，那么需要字符拼接。
                    closeBtn:1,
                    anim: 5,
                    area: ['500px', '400px']

                })
                    layui.form.val("particulars",str.data);
            });

        })
    }

    function add() {
        layui.use(["layer","jquery","table"],function () {
            console.log("点击添加");
            let layer=layui.layer;
            let $=layui.$;
            let table=layui.table;
            let jquery=layui.jquery;
            layer.open({
                type: 1,
                title:"添加订单",
                content: $('#add'), //注意，如果str是object，那么需要字符拼接。
                closeBtn:1,
                area: ['600px', '500px'],
                btn:["确认","取消"],
                success: function(layero, index) {
                    // 重置清空

                    $('#add form')[0].reset();
                        layui.form.render();//必须写


                },
                yes(){
                    let masterVo = layui.form.val("add");
                    console.log(masterVo.cgCode)
                    console.log(masterVo.factory)
                    console.log(masterVo);
                    if (masterVo.cgCode==null||masterVo.cgCode==''||masterVo.factory==null||masterVo.factory==''||masterVo.purchaseBuy==null||masterVo.purchaseBuy==''){
                        alert("不能为空，添加失败")
                        $("#add").click();
                        return false;
                    }else {
                        $.post("add", masterVo)
                            .done(({msg}) => {
                                table.reload("purchaseNote");
                                layer.close(layer.index);
                                layer.msg(msg);
                            });
                    }


                    // $("#add").click();
                }

            })
            //物资编码函数
            $("#cgCode").on("input",function (e) {
                var cgcodeinput=document.getElementById("cgCode");
                var cgcode=cgcodeinput.value;

                $.post("selectByCoCode",{'cgCode':cgcode},function (str) {

                    if (str.data==null){
                        // $("#addform")[1].reset();
                        // layui.form.render();
                        layui.form.val("add",{factory:"",unitprice:""});
                    }
                    layui.form.val("add",str.data);
                    let unitprice=str.data.unitprice;

                })

            });
            //根据数量算总和函数
            $("#purchaseBuy").on("input",function (e) {
                var purchaseinput=document.getElementById("purchaseBuy").value;//数量
                if (document.getElementById("unitprice").value==null){
                    layui.form.val("add",{totalPrice:""});
                }else {
                    var unitprice=document.getElementById("unitprice").value;       //单价
                    var total=purchaseinput*unitprice;
                    console.log(total)
                    layui.form.val("add",{totalPrice:total});
                }

            })


        })

    }

</script>
</body>
<div style="display: none" id="particulars">
    <form class="layui-form" lay-filter="particulars"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
        <div class="layui-form-item">
            <label class="layui-form-label">ID</label>
            <div class="layui-input-block">
                <input type="text" name="purchaseChildId" autocomplete="off" class="layui-input" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">耗材编码</label>
            <div class="layui-input-block">
                <input type="text" name="cgCode" autocomplete="off" class="layui-input" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">单价</label>
            <div class="layui-input-block">
                <input type="text" name="simplePrice" autocomplete="off" class="layui-input" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">采购量</label>
            <div class="layui-input-block">
                <input type="text" name="purchaseBuy" autocomplete="off" class="layui-input" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">合计</label>
            <div class="layui-input-block">
                <input type="text" name="totalPrice" autocomplete="off" class="layui-input" disabled="disabled">
            </div>
        </div>
    </form>
</div>
<div style="display: none" id="add">
    <form class="layui-form" lay-filter="add" >
        <div class="layui-form-item">
            <label class="layui-form-label">耗材编码</label>
            <div class="layui-input-block">
                <input type="text" name="cgCode" id="cgCode" lay-verify="required" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-block">
                <input type="text" name="factory" id="factory" lay-verify="required"   class="layui-input" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">单价</label>
            <div class="layui-input-block">
                <input type="text" name="unitprice" id="unitprice"   lay-verify="required"   class="layui-input" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">采购量</label>
            <div class="layui-input-block">
                <input type="text" name="purchaseBuy" id="purchaseBuy"   lay-verify="required\number"   class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">合计</label>
            <div class="layui-input-block">
                <input type="text" name="totalPrice" id="totalPrice"   lay-verify="required"   class="layui-input" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark"  class="layui-textarea"></textarea>
            </div>
        </div>
    </form>
</div>
<div style="display: none" id="addSTOCK">
    <form class="layui-form" lay-filter="addSTOCK" >
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">所属分院:</label>
            <div class="layui-input-inline">

                <select name="branchCode"  lay-filter="branchCode" lay-verify="">
                    <option value="东">东院区</option>
                    <option value="西">西院区</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-inline">
            <label class="layui-form-label">所属仓库:</label>
            <div class="layui-input-inline">
                <select name="warehousId"  lay-filter="warehouseName" lay-verify="" id="warehouseName">
                    <option value="0">请选择</option>
                </select>

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">物资编码</label>
            <div class="layui-input-block">
                <input type="text" name="cgCode" lay-verify="required"   class="layui-input" disabled="disabled">
            </div>
        </div>
<%--        <div class="layui-form-item">--%>
<%--            <label class="layui-form-label">物资编号</label>--%>
<%--            <div class="layui-input-block">--%>
<%--                <input type="text" name="dictionaryId" id="dictionaryId" lay-verify="required"   class="layui-input" disabled="disabled">--%>
<%--            </div>--%>
<%--        </div>--%>
        <div class="layui-form-item">
            <label class="layui-form-label">数量</label>
            <div class="layui-input-block">
                <input type="text" name="amount" id="amount"   lay-verify="required"   class="layui-input" disabled="disabled">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">校期</label>
            <div class="layui-input-block">
                <input type="text" name="xiaoqi" id="xiaoqi"   lay-verify="required"   class="layui-input">
            </div>
        </div>
    </form>
</div>
</html>
